<template>
  <van-row>
    <van-nav-bar
      title="计件工资管理"
      left-text="返回"
      left-arrow
      @click-left="$router.push('/')"
      @click-right="handleAction"
    >
      <template #right>
        <van-icon name="plus" size="20" />
      </template>
    </van-nav-bar>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-search v-model="page.query.keyword" show-action placeholder="请输入关键字" @search="handleSearch">
        <template #action>
          <div @click="handleSearch">搜索</div>
        </template>
      </van-search>
      <van-dropdown-menu :close-on-click-outside="false">
        <van-dropdown-item v-model="page.query.shiftId" :options="shiftList" @change="handleSearch" />
        <van-dropdown-item v-model="page.query.processId" :options="processList" @change="handleSearch" />
        <van-dropdown-item ref="item" title="筛选">
          <van-cell>
            <template #default>
              <van-field
                readonly
                clickable
                :value="dateRange"
                placeholder="选择日期"
                label="日期"
                input-align="left"
                @click="showDateRangePicker = true"
              />
            </template>
          </van-cell>
          <div style="padding: 20px 20px;display: flex">
            <van-button style="flex: 1" round @click="handleReset">
              重置
            </van-button>
            <van-button style="flex: 1" type="danger" round @click="handleSearch('0')">
              确定
            </van-button>
          </div>
        </van-dropdown-item>
      </van-dropdown-menu>
      <van-list
        v-model="tableLoading"
        :finished="finished"
        finished-text="没有更多了"
        @load="doPage"
      >

        <van-cell v-for="(item,index) in tableData" :key="index" clickable is-link @click="toDetail(item)">
          <template #title>
            <div class="title">
              <div v-html="highlightText(item.title, page.query.keyword)" />
            </div>
            <div class="title right primary">
              <div v-html="highlightText(item.totalPieceRateWage, page.query.keyword)" />
            </div>
          </template>
          <template #label>
            <div v-html="highlightText(item.employeeNames, page.query.keyword)" />
            <div>单价：{{ item.unitPrice }}，总产量：{{ item.quantity }}</div>
          </template>
        </van-cell>
      </van-list>
    </van-pull-refresh>
    <van-popup v-model="showUpdate" position="right" :style="{ width: '100%', height: '100%' }">
      <piece-rate-wage-update-module ref="submitModule" @close="handleClose" />
    </van-popup>
    <van-popup v-model="showCalculate" position="right" :style="{ width: '100%', height: '100%' }">
      <piece-rate-wage-calculate-module ref="calculateModule" @close="handleClose" />
    </van-popup>
    <van-popup v-model="showDetail" position="right" :style="{ width: '100%', height: '100%' }">
      <piece-rate-wage-detail ref="detailModule" @close="handleClose" />
    </van-popup>
    <van-calendar v-model="showDateRangePicker" type="range" :min-date="minDate" :show-confirm="false" @confirm="onConfirmDateRange" />
  </van-row>
</template>
<script>
import { deletePieceRateWageDetail, selectGroupPage } from '@/api/pieceRateWage'
import { Dialog } from 'vant'
import { highlightText } from '@/utils/util'
import PieceRateWageUpdateModule from '@/views/pieceRateWage/pieceRateWageUpdate.vue'
import PieceRateWageCalculateModule from '@/views/pieceRateWage/pieceRateWageCalculate.vue'
import moment from 'moment/moment'
import Moment from 'moment'
import { selectAllShift } from '@/api/shift'
import { selectAllProcess } from '@/api/process'
import PieceRateWageDetail from '@/views/pieceRateWage/pieceRateWageList.vue'

export default {
  name: 'Receipt',
  components: { PieceRateWageDetail, PieceRateWageCalculateModule, PieceRateWageUpdateModule },
  data() {
    return {
      showDetail: false,
      showCalculate: false,
      showUpdate: false,
      tableLoading: false,
      finished: false,
      refreshing: false,
      tableData: [],
      page: {
        page: 1,
        size: 10,
        total: 0,
        query: {
          shiftId: '-1',
          processId: '-1',
          keyword: '',
          dateRange: []
        }
      },
      shiftList: [],
      processList: [],
      showDateRangePicker: false,
      dateRange: '',
      minDate: moment(new Date()).subtract(30, 'years').toDate()
    }
  },
  mounted() {
    this.selectAllShift()
    this.selectAllProcess()
    this.doPage()
  },
  methods: {
    highlightText,
    doPage() {
      this.tableLoading = true
      selectGroupPage(this.page).then(resp => {
        if (resp.success) {
          if (this.refreshing) {
            this.tableData = []
            this.refreshing = false
          }
          this.tableData = [...this.tableData, ...resp.data.records]
          this.page.total = resp.data.total
          this.finished = this.tableData.length >= resp.data.total
          for (let i = 0; i < resp.data.records.length; i++) {
            var record = resp.data.records[i]
            record.title = record.date + ' ' + record.shiftName + ' ' + record.processName
            record.totalPieceRateWage = record.pieceRateWage + '/人'
          }
        }
      }).catch(() => {
        this.finished = true
      }).finally(() => {
        this.tableLoading = false
        this.page.page++
      })
    },
    handleSearch(type) {
      this.tableData = []
      this.page.page = 1
      this.finished = false
      this.doPage()
      if (type === '0') {
        this.$refs.item.toggle()
      }
    },
    onRefresh() {
      this.page.page = 1
      this.doPage()
    },
    toUpdate(row) {
      this.showUpdate = true
      this.$nextTick(() => {
        this.$refs.submitModule.init(row.id)
      })
    },
    toDetail(row) {
      this.showDetail = true
      this.$nextTick(() => {
        this.$refs.detailModule.handleSearch(row.id)
      })
    },
    handleAction() {
      this.showCalculate = true
      this.$nextTick(() => {
        this.$refs.calculateModule.init()
      })
    },
    handleDelete(row) {
      Dialog.confirm({
        title: '删除确认',
        message: '是否删除此信息'
      }).then(() => {
        deletePieceRateWageDetail({ ids: [row.id] }).then(resp => {
          if (resp.success) {
            this.$toast.success('删除成功')
          }
        }).finally(() => {
          this.handleSearch()
        })
      }).catch(() => {})
    },
    selectAllShift() {
      selectAllShift().then(resp => {
        if (resp.success) {
          this.shiftList = [{ text: '全部', value: '-1' }]
          for (let i = 0; i < resp.data.length; i++) {
            var shiftData = resp.data[i]
            const shift = { text: shiftData.name, value: shiftData.id }
            this.shiftList.push(shift)
          }
        }
      })
    },
    selectAllProcess() {
      selectAllProcess().then(resp => {
        if (resp.success) {
          this.processList = [{ text: '全部', value: '-1' }]
          for (let i = 0; i < resp.data.length; i++) {
            var processData = resp.data[i]
            const process = { text: processData.name, value: processData.id }
            this.processList.push(process)
          }
        }
      })
    },
    handleClose() {
      this.showDetail = false
      this.showUpdate = false
      this.showCalculate = false
      this.handleSearch()
    },
    onConfirmDateRange(date) {
      const [start, end] = date
      this.page.query.dateRange = [Moment(start).format('YYYY-MM-DD'), Moment(end).format('YYYY-MM-DD')]
      this.dateRange = Moment(start).format('YYYY-MM-DD') + '至' + Moment(end).format('YYYY-MM-DD')
      this.showDateRangePicker = false
    },
    handleReset() {
      this.dateRange = ''
      this.page.query.dateRange = []
    }
  }
}
</script>

<style scoped>

</style>
